package com.fmdrama;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import com.fmdrama.ui.LogPane;
import com.fmdrama.ui.Status;
import com.fmdrama.util.Configuration;
import com.fmdrama.util.JdbcUtil;
import com.fmdrama.util.LOG;

/**
 *整个程序的入口 
 */
public class Main extends JFrame {
	private static final long serialVersionUID = 1L;

	public static Main self;
	
	public boolean started = false; //是否已开始，已开始为true
	public boolean cateStarted = false; //是否已开始(种类)，已开始为true
	
	private JButton startBtn = new JButton("开始");
	private JButton cateBtn = new  JButton("开始(种类)");
	private JComboBox cateCb = new JComboBox();
	public Status status = new Status("首页");
	public Status cateStatus = new Status("种类");
	private LogPane logPane = new LogPane();
	
	private Crawler crawler = null;
	private CategoryCrawler cateCrawler = null;

	public Main(){
		self = this;
	}
	
	/**
	 * 初始化
	 */
	private void init(){
		this.setTitle("视频信息抓取器");
		this.setSize(900, 600); 
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭窗口的时候，程序退出
		this.setLocationRelativeTo(null);
		this.setLayout(null);
		this.setResizable(false);
		this.setVisible(true); 
		
		startBtn.setBounds(10, 10, 60, 30);
		startBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(started){
					stop();
				}else{
					start();
				}
			}
		});
		cateBtn.setBounds(80, 10, 100, 30);
		cateBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(cateStarted){
					stopCate();
				}else{
					startCate();
				}
			}
		});
		cateCb.setBounds(190, 10, 100, 30);
		cateCb.addItem("4 - 영화");
		cateCb.addItem("1 - 드라마");
		cateCb.addItem("2 - 예능");
		cateCb.addItem("3 - 음악");
		cateCb.addItem("5 - 시사다큐");
		cateCb.addItem("6 - 어리니");
		
		
		status.setBounds(600, 0, 300, 30);
		cateStatus.setBounds(600, 20, 300, 30);
		
		JScrollPane scrollPane = new JScrollPane(logPane);
		scrollPane.setBounds(0, 50, 895, 520);
		
		this.add(startBtn);
		this.add(cateBtn);
		this.add(cateCb);
		this.add(status);
		this.add(cateStatus);
		this.add(scrollPane);
		
		LOG.logPane = logPane;
		SwingUtilities.updateComponentTreeUI(this);
	}
	
	/**
	 * 检查是否已经准备好了，准备好了才能开始
	 * @return
	 */
	private boolean ready(){
		if(!Configuration.load()){
			return false;
		}
		try{
			//测试是否能链接上数据库
			if(JdbcUtil.init()){
				Connection conn = JdbcUtil.getConnection();
				conn.close();
			}else{
				return false;
			}
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("获取数据库链接失败，请检查数据库配置文件和数据库是否正常。");
			return false;
		}
		return true;
	}
	
	/**
	 * 开始
	 */
	private void start() {
		if(ready()){
			crawler = new Crawler(); //实例化爬行者
			crawler.start(); //开始抓取
			started = true;
			updateUi();
			LOG.info("已开始");
		}
	}
	
	/**
	 * 停止
	 */
	private void stop() {
		crawler.stop();
		started = false;
		updateUi();
		LOG.info("已停止");
	}
	
	/**
	 * 更新ui
	 */
	private void updateUi(){
		if(started){
			startBtn.setText("停止");
		}else{
			startBtn.setText("开始");
		}
	}
	
	
	/**
	 * 开始(种类)
	 */
	private void startCate() {
		if(ready()){
			cateCrawler = new CategoryCrawler(cateCb.getSelectedIndex()+2); //注意这里传的是目标网站的种类id
			cateCrawler.start(); //开始抓取
			cateStarted = true;
			updateUiCate();
			LOG.info("已开始(种类)");
		}
	}
	
	/**
	 * 停止(种类)
	 */
	public void stopCate() {
		cateCrawler.stop();
		cateStarted = false;
		updateUiCate();
		LOG.info("已停止(种类)");
	}
	
	/**
	 * 更新ui(种类)
	 */
	private void updateUiCate(){
		if(cateStarted){
			cateBtn.setText("停止(种类)");
		}else{
			cateBtn.setText("开始(种类)");
		}
	}
	
	public static void main(String[] args) {
		Main main = new Main();
		main.init();
	}
}
